package com.proto3.proto;

import com.google.protobuf.Any;

import java.io.*;
import java.util.Scanner;

public class TestWrite {
    public static void main(String[] args) throws IOException {
        //往通讯录当中新增一个联系人
        Contacts.Builder contactsBuilder = Contacts.newBuilder();
        contactsBuilder.addPersons(createPersonInfo());
        //由于每次的build里面就只有一条联系人信息, 所以每次也只能写一条数据 我们需要先从文件中读-->反序列化-->build-->合并到当前的构造器中
        try {
            contactsBuilder.mergeFrom(new FileInputStream("src/main/java/com/proto3/proto/contacts.bin"));
        } catch (FileNotFoundException e) {
            System.out.println("file not find, but create a new file");
        }
        //将通讯录序列化然后写入文件
        OutputStream fileOutputStream = new FileOutputStream("src/main/java/com/proto3/proto/contacts.bin");
        contactsBuilder.build().writeTo(fileOutputStream);
        fileOutputStream.close();

    }

    private static PersonInfo createPersonInfo() {
        PersonInfo.Builder builder = PersonInfo.newBuilder();
        System.out.println("----新增联系人----");
        System.out.println("请输入联系人姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        builder.setName(name);
        builder.setAge(age);
        for (int i = 0; ; i++) {
            System.out.println("输入联系人电话号码" + (i + 1) + "(回车即可完成电话输入)");
            Scanner scanner1 = new Scanner(System.in);
            String phoneNumber = scanner1.nextLine();
            if (phoneNumber.isEmpty()) {
                //用户主动退出了
                break;
            }
            PersonInfo.Phone.Builder phoneBuilder = PersonInfo.Phone.newBuilder();
            phoneBuilder.setNumber(phoneNumber);
            System.out.println("请输入电话类型  0:移动电话 1:家用电话 2:办公电话");
            int type = scanner1.nextInt();
            switch (type) {
                case 0:
                    phoneBuilder.setType(PersonInfo.Phone.PhoneType.MOBILE);
                    break;
                case 1:
                    phoneBuilder.setType(PersonInfo.Phone.PhoneType.HOME);
                    break;
                case 2:
                    phoneBuilder.setType(PersonInfo.Phone.PhoneType.WORK);
                    break;
            }
            builder.addPhones(phoneBuilder.build());
        }
        scanner.nextLine();
        //添加地址信息
        Address.Builder addressBuilder = Address.newBuilder();
        System.out.println("请输入家庭地址");
        java.lang.String home = scanner.nextLine();
        addressBuilder.setHomeAddress(home);
        System.out.println("请输入公司地址");
        String company = scanner.nextLine();
        addressBuilder.setCompanyAddress(company);
        builder.setAddress(Any.pack(addressBuilder.build()));

        //添加其他联系方式
        System.out.println("请选择添加其他联系方式: 1-->qq  2-->wechat ");
        int othercontact = scanner.nextInt();
        if (othercontact == 1) {
            System.out.println("请输入qq号:");
            String qq = scanner.next();
            builder.setQq(qq);
        } else if (othercontact == 2) {
            System.out.println("请输入微信号:");
            String wechat = scanner.next();
            builder.setWechat(wechat);
        } else {
            System.out.println("输入的无效, 添加其他联系方式失败");
        }

        //添加备注信息
        for (int i = 0; ; i++) {
            System.out.println("请输入备注:");
            scanner.nextLine();
            String key = scanner.nextLine();
            if (key.isEmpty()) {
                break;
            }
            System.out.printf("请输入备注内容:");
            String value = scanner.next();
            builder.putRemark(key, value);
        }
        System.out.println("-------添加联系人结束--------");
        return builder.build();

    }

}
